package br.ufpe.cin.msc.jrsj2.recommender.message;

import java.io.StringWriter;
import java.util.List;

import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerConfigurationException;
import javax.xml.transform.TransformerException;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;

import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.xml.sax.Attributes;

import br.ufpe.cin.msc.jrsj2.recommender.persistence.domain.Channel;

public class ChannelMessage extends ApplicationMessage {

	public enum ChannelMessageAction {
		CREATE_CHANNEL, LIST_CHANNEL, DELETE_CHANNEL
	};

	private ChannelMessageAction channelMessageAction;
	private String name;
	private String resource;

	public ChannelMessageAction getChannelMessageType() {
		return this.channelMessageAction;
	}

	public String getChannelName() {
		return this.name;
	}

	public String getResource() {
		return this.resource;
	}

	@Override
	protected void parseMessageElement(String uri, String localName,
			String qName, Attributes attributes) {
		if (qName.equals("channel")) {
			String action = attributes.getValue("action");
			if (action.equals("list")) {
				channelMessageAction = ChannelMessageAction.LIST_CHANNEL;
				responseMessage = "list";
			} else if (action.equals("create")) {
				channelMessageAction = ChannelMessageAction.CREATE_CHANNEL;
				name = attributes.getValue("name");
				resource = attributes.getValue("resource");
			} else if (action.equals("delete")) {
				name = attributes.getValue("name");
				channelMessageAction = ChannelMessageAction.DELETE_CHANNEL;
			}
		}
	}

	@Override
	protected void parseMessageContent(char[] ch, int start, int length) {
	}

	@SuppressWarnings("rawtypes")
	@Override
	public void buildResponseMessage(List objects) {
		DocumentBuilderFactory docFactory = DocumentBuilderFactory
				.newInstance();
		Document document;
		Node root;

		try {
			document = docFactory.newDocumentBuilder().newDocument();
		} catch (ParserConfigurationException e) {
			e.printStackTrace();
			return;
		}
		root = document.createElement("channel-list");
		document.appendChild(root);

		@SuppressWarnings("unchecked")
		List<Channel> channels = objects;

		for (Channel c : channels) {
			Element channel = document.createElementNS(null, "channel");
			channel.setAttributeNS(null, "name", c.getName());
			channel.setAttributeNS(null, "resource", c.getServiceLocation());
			root.appendChild(channel);
		}

		TransformerFactory transformerFactory = TransformerFactory
				.newInstance();
		StringWriter writer = new StringWriter();
		try {
			Transformer transformer = transformerFactory.newTransformer();
			DOMSource source = new DOMSource(document);
			StreamResult result = new StreamResult(writer);
			transformer.transform(source, result);
		} catch (TransformerConfigurationException e) {
			e.printStackTrace();
		} catch (TransformerException e) {
			e.printStackTrace();
		}

		ApplicationMessage.responseMessage = writer.toString();
	}

	@Override
	protected void parseMessageEndElement(String uri, String localName,
			String qName) {
	}

}
